// #ifdef APP-PLUS
import * as UTSRsk from "../uni_modules/uts-rskapi";
// #endif

const printer = uni.requireNativePlugin('LcPrinter');
// const modal = uni.requireNativePlugin('modal');
// var globalEvent = uni.requireNativePlugin('globalEvent');
// let pdaPlugin = uni.requireNativePlugin('Hd-PdaModule');

let deviceType = ''

// 初始化打印机
export function initPrinter() {
	// #ifdef APP-PLUS
	uni.getSystemInfo({
		success: function (res) {
			if(res.deviceBrand == 'qualcomm' && res.deviceModel =='SPDA'){
				deviceType = 'SPDA'
				// 调用异步方法
				if(!UTSRsk){
					uni.showToast({
						title: '当前没有打印设备',
						duration: 2000,
						icon:'none'
					});
					return
				}
				console.log('初始化打印机');
				var ret = UTSRsk.init();
			}else{
				deviceType = 'PDA'
				// 调用异步方法
				if(!printer){
					uni.showToast({
						title: '当前没有打印设备',
						duration: 2000,
						icon:'none'
					});
					return
				}
				console.log('初始化打印机');
				var ret = printer.initPrinter({});
				printer.printEnableMark({
					enable: true
				});
				printer.setConcentration({ //设置打印浓度。font_level 取值范围：1~39。
					level: 10
				});
				printer.setLineSpacing({
					spacing: 1.1
				});
				// printer.setFeedPaperSpace({space: 400})
				// printer.setUnwindPaperLen({length:60})
				printer.getsupportprint();
			}
		}
	});
	// #endif
}

// 关闭打印机
export function closePrinter() {
	// 调用同步方法
	if(deviceType == 'SPDA'){
		UTSRsk.destroy();
	}else{
		if(!printer){
			return
		}
		printer.closePrinter();
	}
}

// 空白测试
export function printLabelTest() {
	console.log('空白测试');
	if(!printer){
		return
	}
	printer.printLine({
		line_length: 5
	});
}
// 物料标签打印
export function printProductLabel(printInfo) {
	if(deviceType == 'SPDA'){
		SPDAPrintProductLabel(printInfo)
		return
	}
	if(!printer){
		uni.showToast({
			title: '当前没有打印设备',
			duration: 2000,
			icon:'none'
		});
		return
	}
	printer.printQR2({
		offset: 100,
		text: printInfo.barCode,
		height: 180
	})
	printer.printText({
		content: '    条        码: ' + printInfo.barCode
	});
	printer.printText({
		content: '\n    物料编号: ' + printInfo.productNo
	});
	printer.printText({
		content: '\n    款        号: ' + (printInfo&&printInfo.sku?printInfo.sku:' ' )
	});
	printer.printText({
		content: '\n    尺        码: ' + (printInfo&&printInfo.yardage?printInfo.yardage:' ' )
	});
	printer.printText({
		content: '\n    数        量: ' + printInfo.quantity + (printInfo&&printInfo.productUnit?('('+printInfo.productUnit+')'):' ')
	});
	printer.printText({
		content: '\n    收料日期 : ' + printInfo.createTime.split(" ")[0]
	});
	// printer.printLine({ //走纸 1 行
	// 	line_length: 8
	// });
	printer.printGoToNextMark();
	// printer.printGoToNextMark1({distance:1000});
}


// 新pda  物料标签打印
export function SPDAPrintProductLabel(printInfo) {
	if(!UTSRsk){
		uni.showToast({
			title: '当前没有打印设备',
			duration: 2000,
			icon:'none'
		});
		return
	}
	UTSRsk.openPrintMark();
	console.log("--------开始打印-----------")
	UTSRsk.PrintChars("\n");
	console.log("\n")
	UTSRsk.PrintSetLayout(1);
	UTSRsk.printQR(printInfo.barCode);
	UTSRsk.PrintSetLayout(0);
	UTSRsk.PrintChars("条    码：" + printInfo.barCode);
	console.log("条    码：" + printInfo.barCode)
	UTSRsk.PrintChars("\n物料编号：" + printInfo.productNo);
	console.log("\n物料编号：" + printInfo.productNo)
	printInfo&&printInfo.sku?printInfo.sku:printInfo.sku = " ";
	UTSRsk.PrintChars("\n款    号：" + printInfo.sku);
	console.log("\n款    号：" + printInfo.sku)
	printInfo&&printInfo.yardage?printInfo.yardage:printInfo.yardage = " ";
	UTSRsk.PrintChars("\n尺    码：" + printInfo.yardage);
	console.log("\n尺    码：" + printInfo.yardage)
	let num = printInfo.quantity
	if(printInfo&&printInfo.productUnit){
		num = num + "("+printInfo.productUnit+")"
	}
	UTSRsk.PrintChars("\n数    量：" + num);
	console.log("\n数    量：" + num)
	UTSRsk.PrintChars("\n收料日期：" + printInfo.createTime.split(" ")[0]);
	console.log("\n收料日期：" + printInfo.createTime.split(" ")[0])
	console.log("--------结束打印-----------")
	UTSRsk.printMark();
}



// 尾数打印
export function printTailLabel(printInfo) {
	// if(deviceType == 'SPDA'){
	// 	SPDAPrintTailLabel(printInfo)
	// 	return
	// }
	// console.log(printInfo)
	if(!printer){
		uni.showToast({
			title: '当前没有打印设备',
			duration: 2000,
			icon:'none'
		});
		return
	}
	printer.printText({
		content: '\n    单  据  号: ' + printInfo.relOrderNo
	});
	printer.printText({
		content: '\n\n    目  的  信  息: ' + printInfo.address
	});
	printer.printText({
		content: '\n\n    SKU 编 号: ' + (printInfo&&printInfo.productNo?printInfo.productNo:' ' )
	});
	printer.printText({
		content: '\n\n    物  料  名  称: ' + (printInfo&&printInfo.productName?printInfo.productName:' ' )
	});
	printer.printText({
		content: '\n\n    数       量: ' + (printInfo&&printInfo.qty?printInfo.qty:' ' )
	});
	printer.printText({
		content: '\n\n    单      位: ' + (printInfo&&printInfo.productUnit?printInfo.productUnit:' ' )
	});
	printer.setFeedPaperSpace({space:500}) 
	printer.setUnwindPaperLen({length:  100 })
	// printer.setUnwindPaperLen(300)
	printer.start();
}


// 新pda  尾数打印
export function SPDAPrintTailLabel(printInfo) {
	if(!UTSRsk){
		uni.showToast({
			title: '当前没有打印设备',
			duration: 2000,
			icon:'none'
		});
		return
	}
	UTSRsk.openPrintMark();
	UTSRsk.PrintSetLayout(0);
	UTSRsk.PrintChars("\n需 求 号：" + printInfo.fbillno);
	UTSRsk.PrintChars("\n\n点    位：" + printInfo.fsitecode);
	printInfo&&printInfo.fprodcode?printInfo.fprodcode:printInfo.fprodcode = " ";
	UTSRsk.PrintChars("\n\n型    体：" + printInfo.fprodcode);
	printInfo&&printInfo.fqty?printInfo.fqty:printInfo.fqty = " ";
	UTSRsk.PrintChars("\n\n需 求 数：" + printInfo.fqty);
	printInfo&&printInfo.fsize?printInfo.fsize:printInfo.fsize = " ";
	UTSRsk.PrintChars("\n\n码    数：" + printInfo.fsize);
	printInfo&&printInfo.fteamname?printInfo.fteamname:printInfo.fteamname = " ";
	UTSRsk.PrintChars("\n\n成型组别：" + printInfo.fteamname);
	UTSRsk.printMark();
}